package com.ruoyi.project.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.domain.InsReagentForm;
import com.ruoyi.project.product.domain.InsReagentFormDTO;
import com.ruoyi.project.product.domain.InsReagentFormDetail;
import com.ruoyi.project.product.service.IInsReagentFormDetailService;
import com.ruoyi.project.product.service.IInsReagentFormService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 试剂单Controller
 *
 * @author taney
 * @date 2020-11-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/product/reagentForm")
public class InsReagentFormController extends BaseController {

    private final IInsReagentFormService iInsReagentFormService;
    private final IInsReagentFormDetailService iInsReagentFormDetailService;

    /**
     * 查询试剂单列表
     */
    @PreAuthorize("@ss.hasPermi('product:reagentForm:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsReagentForm insReagentForm) {
        startPage();
        LambdaQueryWrapper<InsReagentForm> lqw = new LambdaQueryWrapper<InsReagentForm>();
        if (insReagentForm.getProjectId() != null) {
            lqw.eq(InsReagentForm::getProjectId, insReagentForm.getProjectId());
        }
        if (StringUtils.isNotBlank(insReagentForm.getMachineType())) {
            lqw.eq(InsReagentForm::getMachineType, insReagentForm.getMachineType());
        }
        if (StringUtils.isNotBlank(insReagentForm.getItemNo())) {
            lqw.like(InsReagentForm::getItemNo, insReagentForm.getItemNo());
        }
        List<InsReagentFormDTO> list = iInsReagentFormService.listInsReagentFormDTO(lqw);
        list.forEach(item -> {
            LambdaQueryWrapper<InsReagentFormDetail> lqw2 = new LambdaQueryWrapper<InsReagentFormDetail>();
            lqw2.eq(InsReagentFormDetail::getReagentFormId, item.getReagentFormId());
            List<InsReagentFormDetail> insReagentFormDetailList = iInsReagentFormDetailService.list(lqw2);
            item.setInsReagentFormDetailList(insReagentFormDetailList);
        });
        return getDataTable(list);
    }

    /**
     * 导出试剂单列表
     */
    @PreAuthorize("@ss.hasPermi('product:reagentForm:export')")
    @Log(title = "试剂单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsReagentForm insReagentForm) {
        LambdaQueryWrapper<InsReagentForm> lqw = new LambdaQueryWrapper<InsReagentForm>(insReagentForm);
        List<InsReagentForm> list = iInsReagentFormService.list(lqw);
        ExcelUtil<InsReagentForm> util = new ExcelUtil<InsReagentForm>(InsReagentForm.class);
        return util.exportExcel(list, "reagentForm");
    }

    /**
     * 获取试剂单详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:reagentForm:query')")
    @GetMapping(value = "/{reagentFormId}")
    public AjaxResult getInfo(@PathVariable("reagentFormId") Integer reagentFormId) {
        InsReagentForm insReagentForm = iInsReagentFormService.getById(reagentFormId);
        InsReagentFormDTO insReagentFormDTO = new InsReagentFormDTO();
        BeanUtils.copyProperties(insReagentForm, insReagentFormDTO);

        LambdaQueryWrapper<InsReagentFormDetail> lqw = new LambdaQueryWrapper<InsReagentFormDetail>();
        lqw.eq(InsReagentFormDetail::getReagentFormId, insReagentForm.getReagentFormId());
        List<InsReagentFormDetail> insReagentFormDetailList = iInsReagentFormDetailService.list(lqw);
        insReagentFormDTO.setInsReagentFormDetailList(insReagentFormDetailList);

        return AjaxResult.success(insReagentFormDTO);
    }

    /**
     * 新增试剂单
     */
    @PreAuthorize("@ss.hasPermi('product:reagentForm:add')")
    @Log(title = "试剂单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsReagentForm insReagentForm) {
        return iInsReagentFormService.save(insReagentForm) ? AjaxResult.success(insReagentForm) : AjaxResult.error("添加失败");
    }

    /**
     * 修改试剂单
     */
    @PreAuthorize("@ss.hasPermi('product:reagentForm:edit')")
    @Log(title = "试剂单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsReagentForm insReagentForm) {
        return toAjax(iInsReagentFormService.updateById(insReagentForm) ? 1 : 0);
    }

    /**
     * 删除试剂单
     */
    @PreAuthorize("@ss.hasPermi('product:reagentForm:remove')")
    @Log(title = "试剂单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reagentFormIds}")
    public AjaxResult remove(@PathVariable Integer[] reagentFormIds) {
        List<Integer> ids = Arrays.asList(reagentFormIds);
        boolean flag = iInsReagentFormService.removeByIds(ids);
        ids.forEach(iInsReagentFormDetailService::removeByReagentFormId);
        return toAjax(flag ? 1 : 0);
    }

    /**
     * 查询试剂单列表
     */
    @PreAuthorize("@ss.hasPermi('product:reagentForm:getByItemNo')")
    @GetMapping("/getByItemNo")
    public TableDataInfo getByItemNo(InsReagentForm insReagentForm) {
        startPage();
        LambdaQueryWrapper<InsReagentForm> lqw = new LambdaQueryWrapper<InsReagentForm>();
        if (StringUtils.isNotBlank(insReagentForm.getItemNo())) {
            lqw.eq(InsReagentForm::getItemNo, insReagentForm.getItemNo());
        }
        if (StringUtils.isNotBlank(insReagentForm.getMachineType())) {
            lqw.eq(InsReagentForm::getMachineType, insReagentForm.getMachineType());
        }
        List<InsReagentForm> list = iInsReagentFormService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 查询试剂单列表
     */
    @PreAuthorize("@ss.hasPermi('product:reagentForm:listItemNoDetailByProjectAndMachineType')")
    @GetMapping("/listItemNoDetailByProjectAndMachineType")
    public AjaxResult listItemNoDetailByProjectAndMachineType(InsReagentForm insReagentForm) {
        LambdaQueryWrapper<InsReagentForm> lqw = new LambdaQueryWrapper<InsReagentForm>();
        lqw.eq(InsReagentForm::getProjectId, insReagentForm.getProjectId());
        lqw.eq(InsReagentForm::getMachineType, insReagentForm.getMachineType());

        List<InsReagentFormDTO> list = iInsReagentFormService.listInsReagentFormDTO(lqw);
        list.forEach(item -> {
            LambdaQueryWrapper<InsReagentFormDetail> lqw2 = new LambdaQueryWrapper<InsReagentFormDetail>();
            lqw2.eq(InsReagentFormDetail::getReagentFormId, item.getReagentFormId());
            List<InsReagentFormDetail> insReagentFormDetailList = iInsReagentFormDetailService.list(lqw2);
            item.setInsReagentFormDetailList(insReagentFormDetailList);
        });
        return AjaxResult.success(list);
    }
}
